package xddq

import (
	"fmt"
	"time"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

func (xd *XdDriver) CheckWildZoneInfoData(info D.WildZoneInfo) {
	if len(info.TeamName) == 0 {
		info.TeamName = fmt.Sprintf("战力高的来%s", proto.GetRandomNum(2))
		log.Infof("蛮荒妖域-未组队，开始创建队伍[%s]", info.TeamName)
		name := proto.StrToHexDataStr(info.TeamName)
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_ZONE_TEAM_CREATE, fmt.Sprintf("08 %s 12 %02X %s 18 00", xd.UserData.Activity[D.Activity_WILD_ZONE].ID, len(name)/2, name)))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_ZONE_TEAM_CREATE, fmt.Sprintf("08 %s 12 %02X %s 18 01", xd.UserData.Activity[D.Activity_WILD_ZONE].ID, len(name)/2, name)))
		return
	}
	if len(info.Person) == 4 {
		log.Infof("蛮荒妖域-组队完成,结束查看")
		xd.StopTaskByID(proto.S_ACTIVITY_WILD_ZONE_ENTER_PANEL)
	}
	//入队申请处理
	if info.CombatValue > 0 && len(info.Person) < 4 && len(info.Invite) > 0 {
		targetVal := info.CombatValue / 10 * 8
		for _, person := range info.Invite {
			if person.CombatValue > targetVal {
				log.Infof("蛮荒妖域-同意[%s]入队申请,妖力%s", person.Name, D.GetCombatValueDesc(person.CombatValue))
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_ZONE_TEAM_APPLY_AGREE, fmt.Sprintf("08 %s 10 %s", xd.UserData.Activity[D.Activity_WILD_ZONE].ID, person.UserID)))
			} else {
				//log.Infof("蛮荒妖域-拒绝[%s]入队申请,妖力%s", person.Name, D.GetCombatValueDesc(person.CombatValue))
				//xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_ZONE_TEAM_APPLY_REFUSED, fmt.Sprintf("08 %s 10 %s", xd.UserData.Activity[D.Activity_WILD_ZONE].ID, person.UserID)))
			}
		}
	}
}

// 处理蛮荒妖域信息
func (xd *XdDriver) CheckActivityWildInfoData(info D.ActivityWildInfo) {
	if info.NeedStop == true {
		log.Infof("蛮荒妖域-使用香油恢复5点体力,剩余香油%d", xd.UserData.WildOil)
		//xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_PLAYER_RECOVER_ENERGY, fmt.Sprintf("08 %s 12 07 08 AD B4 06 12 01 3%d", xd.UserData.Activity[D.Activity_WILD_ZONE].ID, 1))) //使用香油
		return
	}
	log.Infof("蛮荒妖域-执行探索，activity %s ID %s", xd.UserData.Activity[D.Activity_WILD_ZONE].ID, info.ID)
	//
	xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_PLAYER_EXECUTE_EXPLORE, fmt.Sprintf("08 %s 10 %s", xd.UserData.Activity[D.Activity_WILD_ZONE].ID, info.ID))) //攻击
	xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_ENTER_REGION, fmt.Sprintf("08 %s 10 92 4E 18 02", xd.UserData.Activity[D.Activity_WILD_ZONE].ID)))           //进入第二层
	startTime, err := ParseTimeString("21:00:00")
	if err == nil && time.Now().After(startTime) {
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_ENTER_REGION, fmt.Sprintf("08 %s 10 93 4E 18 01", xd.UserData.Activity[D.Activity_WILD_ZONE].ID))) //消耗青冥石进入第三层
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_ENTER_REGION, fmt.Sprintf("08 %s 10 93 4E 18 02", xd.UserData.Activity[D.Activity_WILD_ZONE].ID))) //击败妖兽进入第三层
	}
	if info.ID == "984E" || info.ID == "994E" {
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_PLAYER_ACTION_EXPLORE, fmt.Sprintf("08 %s 10 01", xd.UserData.Activity[D.Activity_WILD_ZONE].ID))) //标记
	}
	xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_PLAYER_ACTION_EXPLORE, fmt.Sprintf("08 %s 10 02", xd.UserData.Activity[D.Activity_WILD_ZONE].ID))) //绕过去
	xd.ResetTaskByID(proto.S_ACTIVITY_WILD_PLAYER_EXPLORE)
}

// 处理蛮荒妖域队友互助消息
func (xd *XdDriver) CheckActivityWildHelpInfoData(allInfo []D.ActivityWildHelpInfo) {
	for _, info := range allInfo {
		if info.IsMonster {
		} else {
			nowTime := time.Now()
			if info.Time.Before(nowTime) {
				//开始协助攻击
				log.Infof("蛮荒妖域-第%d次协助队友[%s]攻击", info.HelpCount, info.FindUserName)
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_PLAYER_DO_HELP, fmt.Sprintf("08 %s 10 %02X 18 %s", xd.UserData.Activity[D.Activity_WILD_ZONE].ID, info.HelpCount, info.FindUserID)))
			} else {
				leftTime := info.Time.Sub(nowTime)
				if leftTime < 5*time.Minute {
					go func() {
						time.Sleep(leftTime) //开始协助攻击
						log.Infof("蛮荒妖域-第%d次协助队友[%s]攻击", info.HelpCount, info.FindUserName)
						xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_WILD_PLAYER_DO_HELP, fmt.Sprintf("08 %s 10 %02X 18 %s", xd.UserData.Activity[D.Activity_WILD_ZONE].ID, info.HelpCount, info.FindUserID)))
					}()
				} else {
					log.Infof("蛮荒妖域-第%d次协助队友[%s]攻击，剩余时间%s", info.HelpCount, info.FindUserName, proto.PrintRemainTime(leftTime))
				}
			}
		}
	}
}
